package com.tuanbiao.presenter.account;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.abiao.lib_utils.StringUtils;
import com.hzjq.data.hzjq.entity.UserEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.AccountController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.events.OfflineRegisterEvent;
import com.tuanbiao.models.AccountModel;
import com.tuanbiao.utils.IdCardUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/11/14 15:09
 * @description:
 */
public class OfflineRegisterPresenter extends BasePresenter<AccountModel, AccountController.OfflineRegisterView> implements AccountController.OfflineRegisterPresenter {

    private String mIdCard;

    public OfflineRegisterPresenter(AccountModel model, AccountController.OfflineRegisterView rootView) {
        super(model, rootView);
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void initEvent(OfflineRegisterEvent event) {
        mIdCard = event.getIdCard();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        if (!TextUtils.isEmpty(mIdCard))
            mRootView.showIdCard(mIdCard);
    }

    @Override
    public void register(String idCard, String name, String password, String confirmPassword, String companyName
            , String companyCode, String createCompany, String blastCode) {
        if (TextUtils.isEmpty(idCard)) {
            mRootView.showMessage(R.string.offline_register_id_hint);
            return;
        }
        if (!IdCardUtil.IDCardValidate(idCard)) {
            mRootView.showMessage("请输入正确的身份证号码");
            return;
        }
        if (TextUtils.isEmpty(name)) {
            mRootView.showMessage(R.string.offline_register_name_hint);
            return;
        }
        if (TextUtils.isEmpty(password) || password.length() != 6) {
            mRootView.showMessage(R.string.offline_register_password_hint);
            return;
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            mRootView.showMessage(R.string.offline_register_password_confirm_hint);
            return;
        }
        if (TextUtils.isEmpty(companyName)) {
            mRootView.showMessage(R.string.offline_register_company_name_hint);
            return;
        }
        if (TextUtils.isEmpty(companyCode)) {
            mRootView.showMessage(R.string.offline_register_company_code_hint);
            return;
        }
        if (TextUtils.isEmpty(createCompany)) {
            mRootView.showMessage(R.string.offline_register_create_company_hint);
            return;
        }
        if (TextUtils.isEmpty(blastCode)) {
            mRootView.showMessage(R.string.offline_register_blast_code_hint);
            return;
        }
        if (!StringUtils.equals(password, confirmPassword)) {
            mRootView.showMessage("两次输入的密码不一致，请检查");
            return;
        }
        mRootView.showLoading();
        mModel.offlineRegister(idCard, name, password, companyName, companyCode, createCompany, blastCode, new OnBaseResultCallback<UserEntity>() {
            @Override
            public void onSuccess(UserEntity userEntity) {
                Timber.i("register#" + userEntity);
                mRootView.showMessage("注册成功");
                mRootView.registerSuccess(idCard, password);
            }

            @Override
            public void onFinish() {
                mRootView.hideLoading();
            }
        });

    }
}
